package com.giantlizard.cloud.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.giantlizard.cloud.base.entity.PageQuery;
import com.giantlizard.cloud.system.dto.SysUserDto;
import com.giantlizard.cloud.system.entity.SysUser;
import com.giantlizard.cloud.system.mapper.SysUserMapper;
import com.giantlizard.cloud.system.service.SysRoleService;
import com.giantlizard.cloud.system.service.SysUserService;
import com.giantlizard.cloud.system.vo.SysRoleVo;
import com.giantlizard.cloud.system.vo.SysUserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author kangdongwei
* @since 2024-07-28
*/

@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    final SysRoleService roleService;

    public SysUserServiceImpl(SysRoleService roleService) {
        this.roleService = roleService;
    }

    @Override
    public SysUser transForToEntity(SysUserDto dto) throws Exception {
        SysUser entity = new SysUser();
        BeanUtils.copyProperties(dto, entity);
        return entity;
    }

    @Override
    public SysUserDto transForToDto(SysUser entity) throws Exception {
        SysUserDto dto = new SysUserDto();
        BeanUtils.copyProperties(entity, dto);
        return dto;
    }

    @Override
    public QueryWrapper<SysUser> createQueryWrapper(SysUserDto dto, PageQuery page) throws Exception {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(ObjectUtil.isNotEmpty(dto.getUserId()),"user_id", dto.getUserId());
        queryWrapper.like(ObjectUtil.isNotEmpty(dto.getUsername()),"user_name", dto.getUsername());
        queryWrapper.like(ObjectUtil.isNotEmpty(dto.getNickName()),"nick_name", dto.getNickName());
        queryWrapper.eq(ObjectUtil.isNotEmpty(dto.getUserEmail()),"user_email", dto.getUserEmail());
        queryWrapper.eq(ObjectUtil.isNotEmpty(dto.getPhoneNumber()),"phone_number", dto.getPhoneNumber());
        queryWrapper.eq(ObjectUtil.isNotEmpty(dto.getUserSex()),"user_sex", dto.getUserSex());
        queryWrapper.eq(ObjectUtil.isNotEmpty(dto.getPassword()),"password", dto.getPassword());
        queryWrapper.eq(ObjectUtil.isNotEmpty(dto.getLoginIp()),"login_ip", dto.getLoginIp());
        queryWrapper.eq(ObjectUtil.isNotEmpty(dto.getLoginDate()),"login_date", dto.getLoginDate());
        queryWrapper.eq(ObjectUtil.isNotEmpty(dto.getUserStatus()),"user_status", dto.getUserStatus());
        queryWrapper.eq(ObjectUtil.isNotEmpty(dto.getRegisteredTime()),"registered_time", dto.getRegisteredTime());
        return queryWrapper;
    }

    @Override
    public List<SysRoleVo> getRoles(String userId) {
        return roleService.selectRolesByUserId(userId);
    }

    @Override
    public SysUserVo getUserInfo(String username) {
        return this.baseMapper.findUserInfoByUserName(username);
    }

}

